package com.javafamily.familydemo.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    // 当一个接口有多个时候实现类的时候，如果Spring只能选一个实现进行依赖注入时，就选这个数据源（默认数据源）
    @Primary
    @Bean(name = "familyDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.family")
    public DataSource familyDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "family2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.family2")
    public DataSource family2DataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name="familyJdbcTemplate")
    public JdbcTemplate familyJdbcTemplate (@Qualifier("familyDataSource") DataSource datasource) {
        return new JdbcTemplate(datasource);
    }

    @Bean(name="family2JdbcTemplate")
    public JdbcTemplate family2JdbcTemplate(@Qualifier("family2DataSource") DataSource datasource) {
        return new JdbcTemplate(datasource);
    }

}
